//go:build ignore

package main

import "fmt"

func goRoutineSum(s []int, c chan int) {
	total := 0
	for _, v := range s {
		total += v
	}
	c <- total
}

func main() {
	arr := []int{7, 2, 8, -9, 4, 0}
	// make(chan int, buffersize), buffersize 可以设置缓冲的长度
	c := make(chan int)

	// go 开启协程
	go goRoutineSum(arr[len(arr)/2:], c)
	go goRoutineSum(arr[:len(arr)/2], c)
	// x,y均可以从c接收值,c管道维持一个FIFO的队列
	x, y := <-c, <-c
	//fmt.Fprintf("x = %d, y=%d, total =%d", x, y, x+y)
	fmt.Println(x, y, x+y)
}
